//
//  HistoryListItem.swift
//  test1
//
//  Created by Apple on 2021/6/8.
//

/*
 参数：
 image: 历史记录中该页面子项的logo
 title：历史记录中该页面子项的title
 url：历史记录中该页面子项的url
 callBack(可选):使用的回调函数
 */

import SwiftUI

struct HistoryListItem: View {
    @EnvironmentObject var modelData: HistoryListModelData
    var image: Image
    var title: String
    var url: String
    var callBack : ViewCallBack?
    var body: some View {
        HStack{
            Button(action: {
                //callBack?.goToView(by: .IndexView, sender: self, with: [RequestArgumentKey.wd.rawValue:url])
                callBack?.goToView(to: .IndexView, from: .HistoryView, intent: .Default, with: [.wd:url])
//                if let window = UIApplication.shared.windows.first
//                {
//                    window.rootViewController = UIHostingController(rootView: SearchResultPage(url: url, title: title).environmentObject(ModelData()))
//                    window.makeKeyAndVisible()
//                }
            }){
                HStack{
                    image
                        .resizable()
                        .frame(width: 25, height: 25)
                        .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
                    VStack{
                        Text(title)
                            .font(.title3)
                            .frame(width: 220, height: 30, alignment: .leading)
                            .lineLimit(nil)
                            .foregroundColor(.black)
                        Text(url)
                            .frame(width: 220, height: 20, alignment: .leading)
                            .foregroundColor(.blue)
                            .lineLimit(nil)
                    }
                }
                
            }
            Spacer()
            Button(action: {
                modelData.Searchtext = self.url
                modelData.title = self.title
            }){
                Image("goto")
                    .resizable()
                    .frame(width: 17, height: 20)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
            }
        }
    }
}

struct HistoryListItem_Previews: PreviewProvider {
    static var previews: some View {
        HistoryListItem(image: Image("baidu"),title: "历史记录1111111111", url: "https://www.baidu.com").environmentObject(HistoryListModelData())
    }
}
